package infostroy.bookagolf.model;

import infostroy.bookagolf.common.helpers.ValidationHelper;
import infostroy.bookagolf.components.table.GolfIconTableCell;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Andrew on 12.02.14.
 */
public class GolfTableModel extends AbstractTableModel {

    /**
	 * 
	 */
	private static final long serialVersionUID = -8972200306615542143L;
	private List<Object[][]> data;
    private String[] columnNames;
    private GolfIconTableCell[] customColumnNames;

    private List<Integer> editableColumns;

    public GolfTableModel(List<Object[][]> data, String[] columnNames) {
        this.data = data;
        this.columnNames = columnNames;
        editableColumns = new ArrayList<>();
    }

    public GolfTableModel(List<Object[][]> data, GolfIconTableCell[] customColumnNames) {
        this.data = data;
        this.customColumnNames = customColumnNames;
        editableColumns = new ArrayList<Integer>();
    }
    
    public void setData(List<Object[][]> data)
    {
    	this.data = data;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }


    @Override
    public Class<?> getColumnClass(int columnIndex) {
        Object value = getValueAt(0, columnIndex);
        return value == null ? Object.class : value.getClass();
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return editableColumns.contains(columnIndex) || super.isCellEditable(rowIndex, columnIndex);

    }

    @Override
    public String getColumnName(int column) {
        StringBuilder columnName = new StringBuilder();
        if (!ValidationHelper.isNullOrEmpty(columnNames)) {
            columnName.append(columnNames[column]);
        }
        if (!ValidationHelper.isNullOrEmpty(customColumnNames)) {
            columnName.append(customColumnNames[column].getText());
        }
        return columnName.toString();

    }

    @Override
    public int getColumnCount() {
        int length = 0;
        if (!ValidationHelper.isNullOrEmpty(columnNames)) {
            length = columnNames.length;
        }
        if (!ValidationHelper.isNullOrEmpty(customColumnNames)) {
            length = customColumnNames.length;
        }
        return length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data.get(rowIndex)[0][columnIndex];
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        data.get(rowIndex)[0][columnIndex] = aValue;
        fireTableCellUpdated(rowIndex, columnIndex);
    }

    public void setEditableColumnIndex(int columnIndex) {
        this.editableColumns.add(columnIndex);
    }

    public void removeRow(Integer rowIndex) {
        data.remove((int)rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }

    public void refreshModel(List<Object[][]> data) {
        this.data = data;
        fireTableDataChanged();
    }

}




